<?php
 namespace app\common\model;

use traits\model\SoftDelete;

/**
 * 文章模型
 * @package app\common\model
 */
class Article extends Base
{
    use SoftDelete;
    protected $auto = ['keywords','description'];
    protected $insert = ['visits'=>0];
    protected $autoWriteTimestamp = true;
    protected $type = [
        'delete_time'  =>  'timestamp',
    ];

    /**
     * 添加数据
     *
     * @param array $data
     * @return int
     */
    public function addData($data)
    {
        $result = self::allowField(true)->validate(true)->save($data);
        if (false === $result) {
            return ['errno'=>1,'msg'=>self::getError()];
        }
        return self::getLastInsID();
    }
    /**
     * 编辑数据
     *
     * @param mixed $where
     * @param array $data
     * @return int
     */
    public function editData($where, $data)
    {
        $result = self::allowField(true)->validate(true)->save($data, $where);
        if (false === $result) {
            return ['errno'=>1,'msg'=>self::getError()];
        }
        return $result;
    }
    /**
     * 删除数据
     *
     * @param int $id
     * @return
     */
    public function delData($id, $trueDel = false)
    {
        if (is_array($id)) {
            $ids=array_map('intval', $id);
            return self::destroy($ids, $trueDel);
        }
        return self::destroy(intval($id), $trueDel);
    }
    /**
     * 恢复数据
     *
     * @param int $id
     * @return
     */
    public function restoreData($id)
    {
        if (is_array($id)) {
            foreach ($id as $value) {
                self::restore(['id'=>intval($value)]);
            }
            return true;
        }
        return self::restore(['id'=>intval($id)]);
    }
    /**
     * 全部数据
     *
     * @return obj
     */
    public function getAllData($deleted = false)
    {
        if (false===$deleted) {
            return self::select();
        }
        return self::onlyTrashed()->select();
    }
    /**
     * 条件查询数据
     * @param function|array|string $where
     * @param string $order
     * @param string $limit
     * @param boolean $findOrSelect
     * @return array
     */
    public function getDataByWhere($where, $order = '', $limit = '', $findOrSelect = false)
    {
        if (true===$findOrSelect) {
            $data=self::where($where)
                ->find();
        } else {
            $data=self::where($where)
                ->order($order)
                ->limit($limit)
                ->select();
        }
        return $data;
    }

    /**
     * 获取分页
     *
     * @param function|array|string $where
     * @param integer $num
     * @param string $order
     * @return array
     */
    public function getPage($where, $num = 5, $order = '')
    {
        $data=self::where($where)
            ->order($order)
            ->paginate($num, false, ['query' => request()->param()]);
        return $data;
    }
    /**
     * 统计数据
     *
     * @param function|array|string $where
     * @return int
     */
    public function getCount($where = null)
    {
        if (isset($where)) {
            return self::where($where)->count();
        }
        return self::count();
    }
    public function getStatusAttr($value)
    {
        $status = [0=>'未发布',1=>'已发布'];
        return $status[$value];
    }
    public function setKeywordAttr($value)
    {
        return str_replace('、', ',', $value);
    }
    public function setDescriptionAttr($value, $data)
    {
        if (!empty($value)) {
            return $value;
        }
        $des=htmlspecialchars_decode($data['content']);
        $des=subString(strip_tags($des), 0, 200, false);
        return $des;
    }
}
